<template>
    <div class='commonListDouble' v-loading="loading">
        <div class='commonList_title' v-if="isTitle=='Y'" ><i class='fa fa-info-circle'></i>{{title}}</div>
        <div class='commonList_title commonList_title_center' v-if="isApp=='Y'&&isAppTop==''" ><i class='el-icon-arrow-left' @click="returns" style='margin-right:10px;float:left;line-height:30px'>返回</i><i class='fa fa-info-circle'></i>{{title}}</div>
        <div class='commonList_title commonList_title_center2' v-if="isApp=='Y'&&isAppTop=='Y'" ><i class='el-icon-arrow-left' @click="returns" style='margin-right:10px;float:left;line-height:30px'>返回</i><i class='fa fa-info-circle'></i>{{title}}</div>
     
        <search @search-event="searchEvent" :searchStr="searchStr"  ref="searchs" v-if="searchStr.length>0"></search>
        <el-row class='elrows2' v-if="buttonJson.length>0" style='margin-bottom:5px;'>
            <el-col :span="24" style='padding:0'>
                <button-bar :buttonJson="buttonJson" @btn-click="btnClick"></button-bar>
            </el-col>
            
        </el-row>
        <div class='list'>
            
            <el-row class='listComplex_row'>
                <el-col :span="parseInt(lists[0].width)"  class='listComplex_col' style='padding-right:5px;overflow:hidden'>
                    <commonList noclick="N" ref="commonList" :model="model"  @loadDatad="loadDatad"  :model_key="lists[0].modelKey" :sKey2="lists[0].sKey" hidenSearch='Y' @openWin="openWin"  @rowDbClick="rowDbClick" @rowClick="rowClick" @empty="empty" :searchJson="searchStr2" sKey=""  :company="company"  searchs="" listnames=""   @rowMultiClick="rowMultiClick" :appid="appid" ></commonList>
                </el-col>
                 <el-col :span="parseInt(lists[1].width)"  class='listComplex_col' style='padding-left:5px' >
                    <commonList noclick="N" ref="commonList2" :model="model" @loadDatad="loadDatad2" :model_key="lists[1].modelKey" :sKey2="lists[1].sKey" hidenSearch='Y'  @openWin="openWin"  @rowDbClick="rowDbClick"  :searchJson="searchStr3" sKey=""  :company="company"  searchs=""   :appid="appid"  listnames="" v-if="shows"></commonList>
                </el-col>
            </el-row>
        </div>
         
        
    </div>
</template>
<script>
import commonList  from './commonList.vue'
import ButtonBar  from './ButtonBar.vue'
import search  from './search.vue'
export default {
    name:'commonListDouble',
    props:['sKey','sOtherKey','exKey','company','appid','listname','width','filenameChild','button','fieldPath','model_key','model','isApp','isTitle'],
    components: {
        commonList,
        ButtonBar,
        search
    },
    data(){
        return{
            lists:[],
            sKey2:'',
            searchs:'',
            buttonJson:[],
            searchStr:[],
            searchStr2:[],
            searchStr3:[],
            loading:true,
            curRow:null,
            curRows:null,
            shows:false,
            listnames:"",
            title:"",
            isAppTop:localStorage.getItem("isAppTop")
        }
    },
    created(){   
      //  alert(localStorage.getItem("isAppTop"));  
        
        this.loadData();
        window.addEventListener('message', function(messageEvent) {
            
            //sef.goSearch();
            // vue.menuTitle = data;
            // sef.$refs.commonList.isShowTable();
        },false);
    }, 
    mounted(){
        try{
            let el=document.getElementsByClassName("listComplex_row");
            let height=document.getElementsByClassName("list")[0].offsetHeight;
            for(let i=0;i<el.length;i++){
                el[i].style.flexDirection= "row";
            }
        }catch(e){
            console.log("mounted出错----------------------")
        }
        
    },
    methods:{
        loadData(isDevpos){
            this.$post(this.appid,"api/view/commonListComplexJson",{
                sKey:this.sKey,
                sOtherKey:this.sOtherKey,
                
                listname:escape(this.listname),
                key:escape(this.model_key),
                model:escape(this.model)
            }).then(data=>{
                this.lists=data.list;
                this.buttonJson=data.buttonJson;
                this.col=Number(data.col);
                this.searchStr=data.searchJson;
                this.searchStr2=data.searchJson;

                this.title=data.name;
                this.searchStr3=JSON.parse(JSON.stringify(data.searchJson));
                this.listnames=data.listname;
                
                this.$emit("loadDataMaind",this.listnames)
                if(isDevpos=="Y"){
                    this.$refs.commonList.loadData("Y")
                    this.$refs.commonList2.loadData("Y")
                }
            })
        },
        loadDatad(data){
            this.$emit("loadDatad",data)
        },
        loadDatad2(data){

            this.$emit("loadDatad2",data)
        },
          returns(){
                history.back();
            },
         searchEvent(data){
           this.searchStr2=JSON.parse(data);
            
        },
        empty(data){
            this.loading=false;
            let index=-1;
            for(let i=0;i<this.searchStr3.length;i++){
                let searchId=this.searchStr3[i].searchId;
                if(searchId==this.exKey)
                    index=i;
            }
            this.curRow=data;
            this.searchStr3[index].val="-1";
            //this.$refs.commonList2.searchEvent(JSON.stringify(data));
            if(!this.shows){
                this.shows=true;
            }else
                this.$refs.commonList2.goSearch();

        },
        rowClick(data){
            
            this.loading=false;
            data=JSON.parse(data);
            let key=data[this.exKey];
            let index=-1;
            for(let i=0;i<this.searchStr3.length;i++){
                let searchId=this.searchStr3[i].searchId;
                if(searchId==this.exKey)
                    index=i;
            }
            this.curRow=data;
            this.searchStr3[index].val=key;
            //this.$refs.commonList2.searchEvent(JSON.stringify(data));
            if(!this.shows){
                this.shows=true;
            }else
                this.$refs.commonList2.goSearch();
        },
        getScrollTop(){
            return this.$refs.commonList.getScrollTop();
        },
        setScrollTop(top){
            this.$refs.commonList.setScrollTop(top);
        },
        rowMultiClick(data){
            this.curRows=JSON.parse(data);
            this.$emit("rowMultiClick",data);
        },
        rowDbClick(data){
            this.curRow=JSON.parse(data);
            this.curRow.rowIndex=this.index;
            this.$emit("rowDbClick",JSON.stringify(this.curRow));
        },
        openWin(item){
            this.$parent.openWin(item);
        },
        btnClick(data){
                let item=JSON.parse(data);
                let wb_isEdit=item.wb_isEdit;
                let key=item.key;
                let isOpen=true;
                let wb_msg=item.wb_msg;
                let sId="";
                let isOk=true;
                let appid=item.appid;
                let filename=item.filename;
                let exData=item.exData;
                let params=item.params;
                if(item.buttonName.indexOf("打印")!=-1){

                    let sId2="";
                    if(this.curRow){
                        sId2=this.curRow[key]
                    }
                    let item_={};
                    item_.buttonName=item.buttonName;
                    item_.url="/commonPrint?printName="+wb_msg+"&appid="+appid+"&sCurItem="+sId2+"&token="+sessionStorage.getItem("token");
                    
                    this.$parent.openWin(JSON.stringify(item_));
                    
                    isOk=false;
                } 
                if(isOk){
                   
                    if(wb_isEdit=="Y"&&this.curRow==null){
                        isOpen=false;
                        this.$message.error('请先选择一条数据');
                    }else if(wb_isEdit=="Y"){
                        if(!params){
                            let url=item.url;
                            sId=this.curRow[key];
                            
                            url=url.indexOf("?")==-1 ? url+"?sId="+sId:url+"&sId="+sId;
                           
                            item.sId=sId;
                        }
                    }else{
                        item.sId="";
                    }
                    /*if(wb_isEdit=="Y"&&!sId){
                        isOpen=false;
                        this.$message.error('请先选择一条数据');
                    }*/
                    let paramJson={};
                    if(params){
                        let paramss=params.split(",");
                        let keys=key.split(",");
                        if(keys.length==paramss.length){
                            
                           
                            for(let hh=0;hh<keys.length;hh++){
                                let sId="";
                                if(this.curRow)
                                    sId=this.curRow[keys[hh]];
                                if(!sId)
                                    sId="";
                                
                               // let url=item.url;
                               // url=url.indexOf("?")==-1 ? url+"?"+paramss[hh]+"="+sId:url+"&"+paramss[hh]+"="+sId;
                                paramJson[paramss[hh]]=sId;
                              //  item.url=url;
                                 
                            }
                           
                            
                        }
                    }
                    //设置额外的值，给编辑界面用的，比如在列表上有个按钮，需要选中列表一条数据，然后点新增子表的按钮，则主表的主键值需要传到子表外键去
                    if(exData){
                        exData=exData.replace(/''/g,"\"");
                        exData=JSON.parse(exData);
                        for(let key in exData){
                            let val=exData[key];
                            if(this.curRow){
                                exData[key]=this.curRow[val];
                            }
                        }
                        item.exData=exData;
                    }
                    if(isOpen&&wb_msg==""){
                        let url=item.url;
                        url=url.indexOf("?")==-1 ? url+"?appid="+item.appid:url+"&appid="+item.appid;
                        url=url+"&filename2="+item.filename;
                        url=url+"&key="+item.modelKey;
                        url=url+"&fieldPath="+item.field;
                        url=url+"&model="+item.model;
                        item.url=url;
                        item.appid=appid;
                        item.filename2=filename;
                        item.key=item.modelKey;
                        item.fieldPath=item.field;
                        item.params=paramJson;
                        item.param_key=params;

                        let ids_="";
                        try{
                            if(this.curRows.row){
                                for(let i=0;i<this.curRows.row.length;i++){
                                    let id=this.curRows.row[i][key];
                                    ids_=ids_=="" ? id:ids_+","+id;
                                }
                            }
                        }catch(e){}
                        //alert(ids_)
                        item.ids=ids_;//多选，目前只要用于多选后，在点击按钮，打开一个界面
                        try{
                            this.$parent.openWin(JSON.stringify(item));
                        }catch(e){
                            this.$emit("openWin",JSON.stringify(item))
                        }
                        this.$parent.openWin(JSON.stringify(item));
                    }else if(wb_msg!=""){
                        let url=item.url;
                        let key=item.key;
                       // alert(key)
                        let ids="";
                        if(this.curRows.row){
                            for(let i=0;i<this.curRows.row.length;i++){
                                let id=this.curRows.row[i][key];
                                ids=ids=="" ? id:ids+","+id;
                            }
                        }else{
                            ids=this.curRow[key];
                        }
                        if(!ids){
                            this.$alert('请先选择一行数据', '错误提示', {
                                confirmButtonText: '确定',
                                callback: action => {}
                            });
                            return;
                        }
                        this.$confirm(wb_msg, '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            
                            this.$post(this.appid,url,{ids:ids,filename:filename}).then(data=>{
                                if(data[0].status=="+OK"){
                                    this.$message(unescape(data[0].msg));
                                    this.goSearch()
                                }else{
                                    this.$alert(unescape(data[0].msg), '错误提示', {
                                        confirmButtonText: '确定',
                                        callback: action => {}
                                    });
                                }
                            });
                        }).catch(() => {
                                    
                        });
                    }
                }
                
                
               // this.$parent.$parent.postMessage(data,'*');
            },
            goSearch(){
                this.$refs.commonList.goSearch();
                this.$refs.commonList2.goSearch();
            },
            initData(){
                this.$refs.commonList.initData();
                this.$refs.commonList2.initData();
                
            
            },
            rowDbClick(data){
               
                this.curRow=JSON.parse(data);
                this.curRow.rowIndex=this.index;
                this.$emit("rowDbClick",JSON.stringify(this.curRow));
            },
    } 
}
</script> 
<style scoped>
    .commonListDouble{
        height:100%;
        width:100%;
        display: flex;
        flex-direction: column;

        overflow:hidden;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        padding-top:0;
    } 
    .list{
        flex:1;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        display:flex;
        flex-direction: column;
        overflow:hidden;
    }
    .listComplex_row{
        overflow:hidden;
        display:flex;
        flex:1;
        flex-direction: row;
    }
    .el-col{
        padding:5px 0 5px 0;
        flex:1;
    }
  
   .elrows2{
        background:#f9f9f9;    
        margin-top: 5px;
        border: 1px solid #ddd;
        border-radius: 5px;
    }
     .commonList_title{
        height:35px;
        border:1px solid #2294f2;
        padding-left:15px;
        border-bottom:0;
        line-height:35px;
        background:#2294f2;
        color:#fff;
    }
    .commonList_title i{
        margin-right:5px;
    }
   .commonList_title_center{
       text-align: center;
   }
   .commonList_title_center2{
    text-align:center;
    height:60px;
    padding-top:30px;
    box-sizing: border-box;
}
</style>